package com.caiyi.financial.nirvana.sms.filters;

import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.param.SendMsgParam;
import com.caiyi.financial.nirvana.sms.util.SignUtil;
import com.youyu.common.utils.YyAssert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

public class AccountFilter implements Filter {

    @Override
    public void run(FilterChain filterChain) {
        SendMsgParam param = filterChain.getParam();
        String clientIp = filterChain.getIp();
        SmsUser user = filterChain.getSmsUser();
        String ip = user.getIp();
        //ip验证
        if (StringUtils.isNotBlank(ip)) {
            List<String> ipList = Arrays.asList(ip.split(","));
            YyAssert.isFalse(!ipList.contains(clientIp), ReturnCodeEnum.IP_ERROR);
        }
        //签名验证
        YyAssert.isFalse(!SignUtil.verifySign(param, user.getPassword(), param.getSign()), ReturnCodeEnum.REQUEST_SIGN_ERROR);

        filterChain.doFilter();
    }

}
